package com.yh.jandan.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;
import com.jfinal.log.Log4jLog;
import com.jfinal.plugin.activerecord.Db;
import com.yh.jandan.ext.thread.MeiziSaveThread;
import com.yh.jandan.interceptor.SecureInterceptor;
import com.yh.jandan.model.Meizi;
import com.yh.jandan.util.DateUtils;
import com.yh.jandan.util.MD5Util;

public class MeiziController extends Controller {
	
	public static Map < String , Integer > webkey_count = new HashMap < String , Integer > ( ); // 当前key已经提交的数量
	public static Map < String , Integer > webkey_count_max = new HashMap < String , Integer > ( ); // 当前key的限额
	
	Log log = Log4jLog.getLog ( MeiziController.class );
	
	public void index ( ) {
		setAttr ( "t" , getPara ( "t" , "t" ) );
		setAttr ( "ts" , getPara ( "ts" , "3" ) );
		String md5 = getPara ( "md5" , getPara ( 0 , "md5" ) );
		Meizi meizi = Meizi.dao.findFirst ( "select * from meizi where `show`=? and md5=?" , "true" , md5 );
		if ( meizi == null ) {
			render ( "/meizi.jsp?md5=" + md5 );
			return;
		}
		int id = meizi.getInt ( "id" );
		int preid = id , nextid = id;
		try {
			preid = Db.queryInt ( "select max(id) from meizi where  `show`=? and id<?" , "true" , id );
		} catch ( Exception e ) {
			// e.printStackTrace();
			preid = id;
		}
		try {
			nextid = Db.queryInt ( "select min(id) from meizi where  `show`=? and id>?" , "true" , id );
		} catch ( Exception e ) {
			// e.printStackTrace();
			nextid = id;
		}
		
		Meizi preMeizi = Meizi.dao.findById ( preid );
		Meizi nextMeizi = Meizi.dao.findById ( nextid );
		
		setAttr ( "meizi" , meizi );
		setAttr ( "preMeizi" , preMeizi );
		setAttr ( "nextMeizi" , nextMeizi );
		render ( "/meizi.jsp?md5=" + md5 );
	}
	
	@Before ( SecureInterceptor.class )
	public void reg ( ) {
		Map < String , String > msg = new HashMap < String , String > ( );
		msg.put ( "retcode" , "-1" );
		String md5 = getPara ( "md5" , getPara ( 0 , "md5" ) );
		Meizi meizi = Meizi.dao.findFirst ( "select * from meizi where md5=?" , md5 );
		if ( null != meizi ) {
			// meizi.setShow ( "true" ) ;
			// meizi.update ( ) ;
			Db.update ( "update meizi set `show`=? where md5=?" , "true" , md5 );
			msg.put ( "retcode" , "0" );
		}
		renderJson ( msg );
		return;
	}
	
	public void insert ( ) {
		
		Map < String , String > msg = new HashMap < String , String > ( );
		
		String url = getPara ( "meizi.url" , "" ).trim ( );
		String website = getPara ( "meizi.website" , "" ).trim ( );
		String key = getPara ( "meizi.key" , "" ).trim ( );
		String note = getPara ( "meizi.note" , "" ).trim ( );
		
//		if ( url.toLowerCase ( ).indexOf ( website.toLowerCase ( ) ) < 0 ) { //website不匹配
//			msg.put ( "retcode" , "-2" );
//			msg.put ( "retmsg" , "website不匹配[" + website + "]" );
//			renderText ( JsonKit.toJson ( msg ) );
//			return;
//		}
		
		if ( !ok_webkey ( key ) ) {
			msg.put ( "retcode" , "-1" );
			msg.put ( "retmsg" , "error key or out of range[" + key + "]" );
			renderText ( JsonKit.toJson ( msg ) );
			return;
		}
		
		Meizi meizi = new Meizi ( );
		meizi.setSmall ( url );
		meizi.setBig ( url );
		// meizi.setShow ( "true" ) ;
		meizi.setMd5 ( MD5Util.MD5 ( url ) );
		// meizi.setCreatedate ( date2str ( new Date ( ) ) );
		meizi.setCreatedate ( new Date ( ) );
		meizi.setWebsite ( website );
		meizi.setNote ( note );
		
		if ( !meizi.validate ( ) ) {
			msg.put ( "retcode" , "1" );
			msg.put ( "retmsg" , "website错误[" + meizi.toJson ( ) + "]" );
			renderText ( JsonKit.toJson ( msg ) );
			return;
		} else if ( exists ( meizi ) ) {
			msg.put ( "retcode" , "2" );
			msg.put ( "retmsg" , "图片重复[" + meizi.toJson ( ) + "]" );
			renderText ( JsonKit.toJson ( msg ) );
			return;
		}
		// meizi.save ( );
		MeiziSaveThread.hold ( meizi );
		msg.put ( "retcode" , "0" );
		msg.put ( "retmsg" , "成功" + JsonKit.toJson ( meizi ) );
		log.info ( "meizi.insert:" + JsonKit.toJson ( msg ) );
		renderText ( JsonKit.toJson ( msg ) );
	}
	
	private boolean ok_webkey ( String webkey ) {
		if ( !webkey_count_max.containsKey ( webkey ) ) return false;
		int max = webkey_count_max.get ( webkey );
		if ( !webkey_count.containsKey ( webkey ) ) webkey_count.put ( webkey , 1 );
		int jcount = webkey_count.get ( webkey );
		if ( jcount > max ) return false;
		webkey_count.put ( webkey , ++jcount );
		return true;
	}
	
	private boolean exists ( Meizi meizi ) {
		// int i = Meizi.dao.find( "select * from meizi where url=?" ,
		// meizi.getSmall() ).size();
		int i = Meizi.dao.find ( "select md5 from meizi where md5=?" , meizi.getMd5 ( ) ).size ( );
		return i > 0;
	}
	
	private String date2str ( Date date ) {
		return DateUtils.getDateByFormat ( date , DateUtils.YMDHMS_FORMAT );
	}
	
	@Before ( SecureInterceptor.class )
	public void del ( ) {
		Map < String , String > msg = new HashMap < String , String > ( );
		msg.put ( "retcode" , "-1" );
		String md5 = getPara ( "md5" , getPara ( 0 , "md5" ) );
		Meizi meizi = Meizi.dao.findFirst ( "select * from meizi where `show`=? and md5=?" , "true" , md5 );
		if ( null != meizi ) {
			// meizi.setShow ( "false" ) ;
			// meizi.update ( ) ;
			Db.update ( "update meizi set `show`=? where md5=?" , "false" , md5 );
			msg.put ( "retcode" , "0" );
		} else {
			msg.put ( "retmsg" , "妹子丢了没找到!!!" );
		}
		renderJson ( msg );
		return;
	}
	
}
